<template>
  <div class="flex">
    <validator name='v'>
    <form novalidate class="form-horizontal auto">

       <div class="form-group">
       	<label for="f_plan_name" class="col-sm-2 control-lable">计划名称：</label>
        <div class="col-sm-4">
          <input class="form-control" readonly='readonly' id="f_plan_name"
           v-model="item.f_plan_name"  v-validate:f_plan_name='{ required: true }'>
        </div>
        <label for="f_issued" class="col-sm-2 control-lable">是否下发：</label>
          <div class="col-sm-4">
          <v-select :value.sync="f_issued"
              :options='selects' placeholder='请选择'
              close-on-select clear-button></v-select>
       </div>
       </div>
  </form>

      <button type="button" class="btn btn-success width-80" v-if='$v.valid' @click="confirm()">
         <span class="glyphicon glyphicon-floppy-disk"></span> 保存
      </button>
      <div class="span">
        <data-grid :model="model" partial='list' v-ref:grid >
          <template partial='head'>
            <tr>
              <th>用户名</th>
              <th>小区名称</th>
              <th>地址</th>
            </tr>
          </template>
          <template partial='body' >
            <td>{{row.f_user_name}}</td>
            <td>{{row.f_residential_area}}</td>
            <td>{{row.f_address}}</td>
          </template>
        </data-grid>
    </div>
    </validator>
  </div>
</template>

<script>
import co from 'co'
import * as Util from '../Util'

let saveGen = function * (self) {
  self.model.id = self.item.id
  self.model.f_issued = self.f_issued[0]
  self.model.f_issue_time = Util.toStandardTimeString()
  yield self.$post('rs/logic/预约计划下发', {data: self.model}, {resolveMsg: '下发计划成功！', rejectMsg: '下发计划失败！'})
}

let fetchItems = function * (self) {
  let f_plan_id = "f_plan_id='" + self.item.id + "'"
  let plan = yield self.$post('rs/sql/预约计划下发详情',
  { data: {items: '*', tablename: 't_check_plan_item', condition: f_plan_id, groupitem: '', orderitem: 'f_user_id'} },
  { resolveMsg: null, rejectMsg: null })
  self.model.rows = plan.data
}

export default {
  title: '项目编写',
  data () {
    return {
      model: {
        rows: []
      },
      f_issued: '',
      selects: [
        {label: '是', value: '是'},
        {label: '否', value: '否'}
      ]
    }
  },
  props: [ 'item' ],
  methods: {
    confirm () {
      let gen = saveGen(this)
      return co(gen)
    }
  },
  watch: {
    'item' (val) {
      if (this.item) {
        this.f_issued = [this.item.f_issued]
        let ge = fetchItems(this)
        co(ge)
      }
    }
  }
}
</script>
